//
//  ZZMianViewController.m
//  panyurenkou_ios
//
//  Created by heem on 15/4/29.
//  Copyright (c) 2015年 heem. All rights reserved.
//

#import "ZZMianViewController.h"

@interface ZZMianViewController ()<UIAlertViewDelegate,UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webview;
@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSString *urlKey;
@property (strong, nonatomic) NSString *netUrl;

@end

@implementation ZZMianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化url key
    self.urlKey = @"urlKey";
    self.title=@"番禺区数据中心人口专项统计信息系统";
    //内网地址
    self.url = @"http://172.25.242.6:8000/stat-py-web/login";
    //外网地址
    self.netUrl = @"http://zsoftoray.gicp.net:9090/stat-py-web22";
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(selectLeftAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(selectRightAction:)];
    
    self.navigationItem.rightBarButtonItem = rightButton;
    
    // 监听长按事件
    UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressBtn:)];
    [self.navigationController.navigationBar addGestureRecognizer:longPress];
    longPress.allowableMovement = NO;
    longPress.minimumPressDuration = 0.5;
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    self.url = [defaults stringForKey:self.urlKey] ;
    [self loadWebPageWithString :self.url];
    

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma 横屏处理
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            
            interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}

- (void)loadWebPageWithString:(NSString*)urlString

{
    if (!urlString) {
        urlString =self.url;
    }
   
    NSURL *url  = [NSURL URLWithString:urlString];
    
    //缓存url
    NSUserDefaults *defaults =[NSUserDefaults  standardUserDefaults ];
    [defaults setObject:urlString forKey:self.urlKey];
    [defaults synchronize];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
 
    [self.webview loadRequest:request];
    
}

-(void)selectLeftAction:(id)sender
{
    if ([self.webview canGoBack]) {
        [self.webview goBack];
    }
}

-(void)selectRightAction:(id)sender
{
    [self.webview reload];
}

-(void)selectRightEditAction{
    
    UIAlertView *altView = [[UIAlertView alloc]initWithTitle:@"修改访问地址" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    // 设定altView类型
    [altView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
    // 拿到UITextField
    UITextField *tf = [altView textFieldAtIndex:0];
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    //url
    NSString *defaultUrl =[defaults stringForKey:self.urlKey];
    //NSLog(@"url txxt:%@",defaultUrl);
    tf.text =defaultUrl;
    // 设置textfield 的键盘类型。
    tf.keyboardType = UIKeyboardTypeDefault;
    // 显示 alertView
    [altView show];
    
}


//监听点击时间 代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([btnTitle isEqualToString:@"取消"]) {
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }else if ([btnTitle isEqualToString:@"确定"] ) {
        UITextField *tf = [alertView textFieldAtIndex:0];
        [self loadWebPageWithString :tf.text];
        
    }
}


-(void)longPressBtn:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        [self selectRightEditAction];
    }else if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
        //长按事件结束
    }
}

@end
